<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <el-form inline :model="searchForm" ref="searchForm">
          <el-form-item label="场馆" prop="venue">
            <el-select v-model="searchForm.id">
              <el-option
                v-for="(oVal, oIdx) of type"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList()">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrEdit = true"
            >新增</el-button
          >
        </div>
      </template>

      <el-table :data="tableData" border>
        <el-table-column prop="name" label="场馆" />
        <el-table-column prop="start_time" label="时间段">
          <template #default="{ row }">
            {{ `${row.start_time} ~ ${row.end_time}` }}
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="openEdit(row)"
              >编辑</el-button
            >
            <el-button type="text" class="red" @click="delVenueTimeById(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="pagination.page_size"
        :total="tableTotal"
        @size-change="(val) => (pagination.page_size = val)"
        @current-change="(val) => (pagination.page = val)"
      />
    </footer>

    <el-dialog
      :title="(builingForm.$id ? '编辑' : '新增') + '场次信息'"
      :visible.sync="showAddOrEdit"
      width="666px"
      @closed="resetForm()"
    >
      <el-form
        :model="builingForm"
        ref="builingForm"
        label-width="5em"
      >
        <el-form-item prop="$id" hidden />
        <el-form-item label="场次名称" prop="price">
          <el-input
            v-model="builingForm.name"
            placeholder="请输入场次名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="场馆" prop="venue">
          <el-select v-model="builingForm.venv_id">
            <el-option
              v-for="(oVal, oIdx) of type"
              :key="oIdx"
              v-bind="oVal"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="价格" prop="price">
          <el-input
            v-model="builingForm.price"
            placeholder="请输入价格"
          ></el-input>
        </el-form-item>

        <el-form-item label="开放时间" prop="$time">
          <el-time-picker
            is-range
            v-model="builingForm.time"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="HH:mm:ss"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          @click="submitData()"
          :loading="loading.update"
          >确 定</el-button
        >
      </template>
    </el-dialog>

  </div>
</template>

<script>
import API from "@/api";
import { table, addOrEdit } from "@/utils/mixins";
export default {
  mixins: [table, addOrEdit],
  data: () => ({
    showAddOrEdit:false,
    isLoading: true,
    tableData:[],
    tableTotal: 0,
    isKey: false,
    type:[],
    searchForm: {
      id:1,
      page: 1, // 当前页码
      per_page: 20, // 每页条数
    },
    builingForm: {
      $id:'',
      name:"",
      time:'',
      start_time:"",
      end_time:"",
      price:'',
      venv_id:1,
      seq:1,
    },

  }),

  created() {
    API.getVenueList({ per_page: 100 })
      .then(({ data }) => {
        let type=data.items.map(item=>{
          return {label:item.venv_name,value:item.id}
        })
        this.type=type
        this.getList()
      })
      .catch((err) => {});
  },

  methods: {
    // 获取列表
    getList(){
      this.isLoading=true
      API.getVenueTimeList(this.searchForm.id)
        .then(({data}) => {
          this.tableData=data
          this.tableTotal=data.length
        }).catch((err) => {
          this.tableData=[]
          this.tableTotal=0
        }).finally(() =>{
          this.isLoading = false;
        })
    },

    // 分页
    changeSize(val){
      this.searchForm.per_page = val
      this.searchForm.page = 1
      this.getList()
    },

    // 分页
    changeCurrent(val){
      this.searchForm.page = val
      this.getList()
    },

    // 编辑场次
    openEdit(obj){
      this.builingForm.$id=obj.id
      this.builingForm.name=obj.name
      this.builingForm.price=obj.price
      this.builingForm.venv_id=obj.venv_id
      this.builingForm.time=[obj.start_time,obj.end_time]
      this.showAddOrEdit=true
    },

    // 提交
    submitData(){
      if(!this.builingForm.name.trim()) return this.$message.error("名称不可为空~~");
      if(!this.builingForm.price.trim()) return this.$message.error("价格不可为空~~")
      if(!this.builingForm.venv_id) return this.$message.error("请选择场馆~~")
      if(this.builingForm.time.length!=2) return this.$message.error("请选择时间段~~")

      let data={
        name:this.builingForm.name,
        start_time:this.builingForm.time[0],
        end_time:this.builingForm.time[1],
        price:this.builingForm.price,
        venv_id:this.builingForm.venv_id,
        seq:1,
      }

      if(this.builingForm.$id){
        let id=this.builingForm.$id
        API.editVenueTime(id,data)
          .then(({data}) => {
            this.$message.success("编辑成功！");
          }).catch((err) => {
          }).finally(() =>{
            this.resetForm()
            this.getList()
          })
      }else{
         API.addVenueTime(data)
          .then(({data}) => {
            this.$message.success("新增成功！");
          }).catch((err) => {
          }).finally(() =>{
            this.resetForm()
            this.getList()
          })
      }
    },

    // 删除活动
    delVenueTimeById(id){
      let _that=this
      this.$confirm(`是否删除改条数据？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
      .then(() => {
        API.delVenueTime(id)
        .then(({data}) => {
          this.$message.success("删除成功！");
        }).catch((err) => {
        }).finally(() =>{
          _that.getList()
        })
      })
      .catch(() => {
      });
    },

    resetForm(){
      this.builingForm.$id=''
      this.builingForm.name="",
      this.builingForm.price="",
      this.builingForm.seq=1,
      this.builingForm.venv_id=1,
      this.builingForm.time=[],
      this.showAddOrEdit=false
    },
  },
};
</script>

<style lang="scss" scoped></style>
